package com.blog.mapper;

import com.blog.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {

    /**
     * 获取前端系统首页要显示的所有商品信息
     * 基于@Select注解定义SQL查询语句，
     * 当sql语句中的字段名与要存储到的对象属性名不一致时，
     * 可以通过@Result注解定义映射规则。
     * @return
     */
    @Select("select id,title,url,price,old_price,sale_count from product")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectIndex();


    /**
     * 基于商品id查询商品url信息
     * @param id
     * @return
     */
    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);


    /**
     * 基于商品分类查询商品信息
     * @param cid 商品分类id
     * @return
     */
    @Select("select id,title,url,price,old_price,sale_count from product where category_id=#{cid}")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectByCid(int cid);

    /**
     * 获取销量比较好的商品
     * @return
     */
    @Select("select id,title,sale_count from product" +
            " order by sale_count desc limit 0,6")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectTop();

    /**
     * 在前端页面可以搜索具体商品
     * @param wd
     * @return
     */
    @Select(" select id,title,url,price,old_price,sale_count " +
            " from product " +
            " where title like concat('%',#{wd},'%')")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    List<Product> selectByWd(String wd);

    /**
     * 基于商品id查询商品信息
     * @param id
     * @return
     */

    @Select("select id,title,url,price,old_price,sale_count,view_count from product where id=#{id}")
    @Result(column = "old_price",property = "oldPrice")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "view_count",property = "viewCount")
    Product selectById(int id);



    //========================后端系统===========================

    /**
     * 查询后端系统中的商品信息
     * @return
     */
    //Result注解的作用是当属性名和表字段名不一致时进行的指定
    @Select("select id,title,url,price,sale_count,view_count,created from product")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "view_count",property = "viewCount")
    List<Product> selectAdmin();

    /**
     * 添加新的商品信息
     * @param product
     */
    @Insert("insert into product values(null,#{title},#{url},#{price},#{oldPrice}" +
            ",0,#{saleCount},#{created},#{categoryId})")
    void insert(Product product);

    /**
     * 基于id删除商品信息
     * @param id
     */
    @Delete("delete from product where id=#{id}")
    void deleteById(int id);


    /**
     * 更新商品浏览量
     */
    @Update("update product set view_count=view_count+1 where id=#{id}")
    void updateViewCount(int id);

}
